package top.buyibuyi.course.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.pojo.PageBean;
import top.buyibuyi.course.pojo.Semester;
import top.buyibuyi.course.service.admin.SelectSemesterService;

import java.util.List;

@RestController
@RequestMapping("/semester")
@CrossOrigin
public class SelectSemesterController {

    @Autowired
    private SelectSemesterService selectSemesterService;

    @PostMapping("/selectSemester/{page}/{limit}")
    public R selectSemester(@PathVariable int page, @PathVariable int limit){
        PageBean<Semester> adminPageBean = new PageBean<>();
        System.out.println("页面==》"+page+"页面大小==》"+limit);
        //对pageBean的七个属性赋值
        //页大小
        adminPageBean.setLimit(limit);
        //总数居条数
        int count = selectSemesterService.selectCount();
        System.out.println("kkkkkkk"+count);
        adminPageBean.setTotal(count);
        //设置当前页
        adminPageBean.setPage(page);
        //是否有上一页
        if(page>1){
            adminPageBean.setHasPrevPage(true);
        }else {
            adminPageBean.setHasPrevPage(false);
        }
        //是否有下一页
        if(adminPageBean.getPage() >= adminPageBean.getTotalPage()){
            adminPageBean.setHasNextPage(false);
        }else {
            adminPageBean.setHasNextPage(true);
        }
        //设置数据
        try {
            int startPage = 0;
            startPage = (page-1)*limit;
            System.out.println(startPage);
            List<Semester> semesters = selectSemesterService.selectSemester(startPage, limit);
            adminPageBean.setPageRecode(semesters);
            return R.ok().data("data",adminPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("....");
        }
    }


    @GetMapping("/selectSemesterById/{id}")
    public R selectSemesterById(@PathVariable int id){
        try {
            Semester semester = selectSemesterService.selectSemesterById(id);
            return R.ok().data("data",semester);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
